#include <string>
#include <cstdlib>
#include <vector>
#include <array>
#include <memory>
#include "vmath.h"
#include "glad/gl-headers.h"

#include "glslprogram.h"
#include "texture.h"
#include <model.h>
#include "node.h"
#include "objectfactory.h"

class Waterwave: public Node
{
public:
    Waterwave();
protected:
    bool startup();
    void render(double currentTime, double difTime);
    void shutdown();
protected:
    GLSLProgram program;
    GLuint vao;
    GLuint vbo;
};

Waterwave::Waterwave():
    vao(0)
{
    nodeName = "Waterwave";
}

bool Waterwave::startup()
{
    if (PerfWindow::glVersion < 330) {
        printf("Failed! The GL version is less than 3.3 !\n");
        return false;
    }

    bool ok = program.build("../media/shaders/waterwave/waterwave.vs.glsl", "../media/shaders/waterwave/waterwave.fs.glsl");
    if (!ok)
        return false;

    vao = 0;

    makeScreenVao(vao,vbo);

    return true;
}

void Waterwave::render(double currentTime, double difTime)
{
    (void)difTime;
    glDisable(GL_DEPTH_TEST);
    static const GLfloat gray[] = { 0.0f, 0.8f, 0.2f, 0.0f };

    glClearBufferfv(GL_COLOR, 0, gray);

    program.use();

    float tf = (float)currentTime;
    program.setUniform1f("iTime", tf);
    program.setUniform2f("iResolution", getWidth(), getHeight());

    glBindVertexArray(vao);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}

void Waterwave::shutdown()
{
    program.clear();
    glUseProgram(0);
    glBindVertexArray(0);
    glDeleteVertexArrays(1, &vao);
    glDeleteBuffers(1,&vbo);
}

REGISTER_OBJECT(Node, Waterwave)
